package com.xhw.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xhw.domain.entity.Person;
import com.xhw.web.annotation.AllowAnonymous;
import com.xhw.web.controller.command.PersonPatchCommand;

import wh.coach.web.controller.result.Result;

/**
 * @Author xhw
 * @Date 2023/8/14
 * @Version 1.0
 */
@RestController
public class PersonsController {

    public static final String MANAGER_TOKEN = "iammanager";
    private static final Map<Long, Person> personStore = new HashMap<>();
    private long id = 0;

    @PostMapping("/persons")
    public Long add(final @RequestBody Person person, final @RequestHeader("Access-Token") String token) {
//        checkToken(token);

        person.setId(++id);

        personStore.put(person.getId(), person);

        return person.getId();
    }

    @DeleteMapping("/persons/{id}")
    public Result<Void> delete(final @PathVariable("id") Long id) {
        personStore.remove(id);

        return Result.ok();
    }

    @GetMapping("/error")
    public Result<String> error() {
        throw new RuntimeException("demo error");
    }

    /**
     * 添加一个person，返回一个id
     *
     * @param searchName
     * @return
     */
    @GetMapping("/persons")
    public Result<List<Person>> find(@RequestParam("name") String searchName) {
        //1.如果没有设置searchName，返回所有的person
        List<Person> persons = new ArrayList<>();

        for (Person person : personStore.values()) {
            if (person.getName().equals(searchName)) {
                persons.add(person);
            }
        }

        return Result.ok(persons);
    }

    @GetMapping("/persons/{id}")
    public Result<Person> get(final @PathVariable("id") Long id) {
        return Result.ok(personStore.get(id));
    }

    @AllowAnonymous // 允许匿名访问
    @GetMapping("/ok")
    public Result<String> ok() {
        return Result.ok("hello");
    }

    @PatchMapping("/persons/{id}")
    public Result<Void> patch(final @PathVariable("id") Long id, final @RequestBody PersonPatchCommand patch) {
        Person person = personStore.get(id);
        if (person == null) {
            throw new RuntimeException("Person不存在该id");
        }

        patch.patchNameIfPresent(person::setName);
        patch.patchAgeIfPresent(person::setAge);

        personStore.put(id, person);
        return Result.ok();
    }

    @PutMapping("/persons/{id}")
    public Result<Void> update(final @PathVariable("id") Long id,
                               final @RequestBody Person person) {
        person.setId(id);
        personStore.put(person.getId(), person);
        return Result.ok();
    }

    private void checkToken(final String token) {
        if (!Objects.equals(token, MANAGER_TOKEN)) {
            throw new RuntimeException("token 错误");
        }
    }
}
